package com.aizhixin.lab.remote;

import com.aizhixin.lab.account.domain.ZhiXinUserDomain;
import com.aizhixin.lab.config.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Component
public class ZhixinClient {
    @Autowired
    private Config config;

    public ZhiXinUserDomain getZXUser(String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", token);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
        String queryPath = config.getZhiXinApiPath() + config.getZhiXinApiCurrentUser();
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<ZhiXinUserDomain> responseEntity = restTemplate.exchange(queryPath, HttpMethod.GET, entity, ZhiXinUserDomain.class);
            ZhiXinUserDomain domain = responseEntity.getBody();
            log.info("id:"+domain.getId()+",login:"+domain.getLogin());
            return domain;
        } catch (Exception e) {
            log.warn("Exception", e);
            return null;
        }
    }
}
